import 'package:microbject/core/runtime/lang/class/type_declare.dart';

/// 测速工具类。
@TypeDeclare('Speed')
class Speed extends Object {
  /// 开始时间
  int startTick = 0;

  /// 结束时间
  int endTick = 0;

  /// 当前间隔
  int current = 0;

  /// 执行间隔
  int span = 0;

  /// 最小间隔
  int? spanMin;

  /// 最大间隔
  int? spanMax;

  /// 参数集合
  //  any parameters;

  /// 构造处理
  ///
  /// @param parameters 参数集合
  Speed() {
    // 设置属性
    // this.startTick = 0;
    // this.endTick = 0;
    // this.span = 0;
    // this.spanMin = Number.MAX_VALUE;
    spanMin = 999999;
    spanMax = 0;
    //     // this.parameters = parameters;
    startTick = DateTime.now().millisecondsSinceEpoch;
  }

  /// 开始处理
  void begin() {
    var now = DateTime.now();
    startTick = now.millisecondsSinceEpoch;
  }

  /// 结束处理
  void end() {
    endTick = DateTime.now().millisecondsSinceEpoch;
    int current = this.current = endTick - startTick;
    span += current;
    // if (current < this.spanMin) {
    //   this.spanMin = current;
    // }
    // if (current > this.spanMax) {
    //   this.spanMax = current;
    // }
  }

  /// 重置数据
  void reset() {
    startTick = 0;
    endTick = 0;
    span = 0;
  }

  /// 记录运行信息
  ///
  /// @return 时间间隔
  int record() {
    end();
    return current;
  }

  /**
    * 获得字符串。
    *
    * @return 字符串
    */
  //  String toString() {
  //     return this.current + '/' + this.span + ' (' + this.spanMin + ' - ' + this.spanMax + ')';
  //  }
}
